/*****************************
* ANALYSIS

Name: Gaurav Chiplunkar
Date: April 2020
*****************************/
clear all
set more off
set maxvar 10000
set scheme s1color
set matsize 4500
set checksum off

cap cd "/Users/chiplunkarg/Library/CloudStorage/Dropbox/Project AP Contract/estimation"
cap cd "D:\Dropbox\Project AP Contract\estimation"
global dropbox `c(pwd)'

global filename = "CalibrationResults_1s.csv"
cd "$dropbox/results/NewModel"

global nu = 3
global rho = 0.738 

**************************
* INSHEETING THE DATA 
**************************
insheet using "$dropbox/newmodel/Sigmastar_1sector.csv", clear 

rename v1 indid 
rename v2 nic
rename v3 sigma 
rename v4 se
rename v5 theta 
rename v6 phi 
rename v7 bc 
rename v8 br
rename v9 Ei
rename v10 Er
rename v11 sd_sigma
rename v12 sd_se
rename v13 sd_theta 
rename v14 sd_phi 
rename v15 sd_bc 
rename v16 sd_br
rename v17 N

gen bcbr = bc/br
STOP 


*---------------------------------
* SUMMARY BY INDUSTRIAL CATEGORIES
*---------------------------------
foreach var of varlist Ei Er {
	qui summ `var', de 
	gen z`var'= `var'/`r(p50)'
}

*Entire economy
preserve
gcollapse (p50) theta bc br zEi zEr ///
			(p25) lb_theta = theta lb_bc = bc lb_br = br lb_Ei = zEi lb_Er = zEr ///
			(p75) ub_theta = theta ub_bc = bc ub_br = br ub_Ei = zEi ub_Er = zEr
tempfile whole 
save `whole', replace 
restore 

*By industrial categories 
gcollapse (p50) theta bc br zEi zEr ///
			(p25) lb_theta = theta lb_bc = bc lb_br = br lb_Ei = zEi lb_Er = zEr ///
			(p75) ub_theta = theta ub_bc = bc ub_br = br ub_Ei = zEi ub_Er = zEr, by(ind_cat)
append using `whole'
replace ind_cat = 5 if missing(ind_cat)

la def lindcat 1 "Food, Textiles and Apparel" 2 "Wood, Rubber and Plastic" 3 "Metals and Chemicals" 4 "Machinery and Transport" 5 "All industries"
la val ind_cat lindcat

order 	ind_cat lb_theta theta ub_theta ///
		lb_bc bc ub_bc ///
		lb_br br ub_br ///
		lb_Ei zEi ub_Ei ///
		lb_Er zEr ub_Er 

outsheet using $filename, comma replace 


*Probability Function 
keep indid theta
gen N = 1
reshape wide theta, i(N) j(indid)
expand 100
gen x = _n 

global fmax = 30
global fmin = 1
gen l = $fmin + ($fmax - $fmin) *(x-1)/(100-1)
order l x 

forval i = 1/19 {
	gen tx`i' = 1-l^(1-1/theta`i') * l^(-$rho)
}
egen p25 = rowpctile(tx*), p(25)
egen p50 = rowpctile(tx*), p(50)
egen p75 = rowpctile(tx*), p(75)

twoway 	(line p50 l, lcolor(black) lw(medthick)) ///
		(line p25 l, lcolor(black) lw(medthick) lp(dash)) ///
		(line p75 l, lcolor(black) lw(medthick) lp(dash)) ///
		, xtitle("Firm size (Labor)") ytitle("Implied Size-Based Penalty") ///
		legend(off)
graph export "ProbDetect.png", replace 
		
		


